package com.hc.summary.view;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;

import com.hc.summary.R;

import java.util.ArrayList;
import java.util.List;

public class CommonDialogFragment extends DialogFragment {

    private static final String TAG = "CommonDialogFragment";
    private Builder builder;

    private CommonDialogFragment(Builder builder) {
        this.builder = builder;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(builder.layoutId, container);
        initViews(root);
        return root;
    }

    /*Dialog全屏时可能需要去除内边距*/
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_FRAME, R.style.CommonDialogFragmentDefaultStyle);
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            Window window = dialog.getWindow();
            if (window != null) {
                if (builder.backgroundResId != -1) {
                    window.setBackgroundDrawableResource(builder.backgroundResId);
                }
                WindowManager.LayoutParams layoutParams = window.getAttributes();
                if (builder.width == -3 && builder.height == -3) {
                    layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
                    layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;

                } else {
                    layoutParams.width = builder.width;
                    layoutParams.height = builder.height;
                }
                window.setAttributes(layoutParams);
            }
        }
    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        super.onDismiss(dialog);
        if (builder.eventListener != null) {
            builder.eventListener.onDialogDismiss();
        }
    }

    public void show(FragmentActivity fragmentActivity) {
        if (!isResumed()) {
            super.showNow(fragmentActivity.getSupportFragmentManager(), TAG);
            if (builder.eventListener != null) {
                builder.eventListener.onDialogShowing(requireView());
            }
        }
    }

    private void initViews(View root) {
        setCancelable(builder.cancelable);
        if (builder.eventListener != null) {
            View cancelViewId = root.findViewById(builder.cancelViewId);
            View confirmViewId = root.findViewById(builder.confirmViewId);
            if (cancelViewId != null) {
                cancelViewId.setOnClickListener(v -> builder.eventListener.onCancelViewClick(CommonDialogFragment.this, root));
            }
            if (confirmViewId != null) {
                confirmViewId.setOnClickListener(v -> builder.eventListener.onConfirmViewClick(CommonDialogFragment.this, root));
            }
            if (builder.otherViewIds != null) {
                for (int id : builder.otherViewIds) {
                    View view = root.findViewById(id);
                    if (view != null) {
                        view.setOnClickListener(v -> builder.eventListener.onOtherViewClick(CommonDialogFragment.this, id, root));
                    }
                }
            }
        }
    }

    public static class Builder {

        private int layoutId;
        private int cancelViewId;
        private int confirmViewId;
        private int backgroundResId = -1;
        private int width = -3;
        private int height = -3;
        private List<Integer> otherViewIds;
        private boolean cancelable;
        private ViewEventListener eventListener;

        public Builder setLayoutId(int layoutId) {
            this.layoutId = layoutId;
            return this;
        }

        public Builder setCancelViewId(int cancelViewId) {
            this.cancelViewId = cancelViewId;
            return this;
        }

        public Builder setConfirmViewId(int confirmViewId) {
            this.confirmViewId = confirmViewId;
            return this;
        }

        public Builder setBackgroundResId(int backgroundResId) {
            this.backgroundResId = backgroundResId;
            return this;
        }

        public Builder setWidth(int width) {
            this.width = width;
            return this;
        }

        public Builder setHeight(int height) {
            this.height = height;
            return this;
        }

        public Builder setOtherViewIds(int... otherViewIds) {
            if (otherViewIds.length > 0) {
                if (this.otherViewIds == null) {
                    this.otherViewIds = new ArrayList<>();
                }
                for (int id : otherViewIds) {
                    this.otherViewIds.add(id);
                }
            }
            return this;
        }

        public Builder setCancelable(boolean cancelable) {
            this.cancelable = cancelable;
            return this;
        }

        public Builder setViewEventListener(ViewEventListener eventListener) {
            this.eventListener = eventListener;
            return this;
        }

        public CommonDialogFragment build() {
            return new CommonDialogFragment(this);
        }
    }

    public interface ViewEventListener {

        default void onCancelViewClick(CommonDialogFragment dialogFragment, View root) {
            dialogFragment.dismiss();
        }

        default void onConfirmViewClick(CommonDialogFragment dialogFragment, View root) {
            dialogFragment.dismiss();
        }

        default void onOtherViewClick(CommonDialogFragment dialogFragment, int otherViewId, View root) {
            dialogFragment.dismiss();
        }

        default void onDialogShowing(View root) {
        }

        default void onDialogDismiss() {
        }
    }
}
